package com.yanwq.sayhi.service;

import com.yanwq.sayhi.bean.Club;
import com.yanwq.sayhi.bean.User;
import com.yanwq.sayhi.repository.ClubRepo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.JpaSort;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 作者：yanweiqiang。
 * 日期：2016/6/16 0016。
 */
@Service
public class ClubService {
    @Autowired
    private ClubRepo repo;

    public Club add(Club club) {
        return repo.save(club);
    }

    public Club update(Club club) {
        return repo.save(club);
    }

    public List<Club> listByName(String name, int page, int size) {
        Page<Club> clubs = repo.findByNameOrderByCreateTimeDesc(String.format("%%%s%%", name), new PageRequest(page, size));
        return clubs.getContent();
    }

    public List<Club> listAll(long userId, int page, int size) {
        User user=new User();
        user.setId(userId);
        Page<Club> clubs = repo.listByUnUser(user, new PageRequest(page, size));
        return clubs.getContent();
    }

    public List<Club> listByPermission(int permission, int page, int size) {
        Page<Club> clubs = repo.findByPermissionOrderByCreateTimeDesc(permission, new PageRequest(page, size));
        return clubs.getContent();
    }
}
